import React from 'react';
import PropTypes from 'prop-types';
import {Card, Button, Row, Col, Divider} from 'antd';
import {DATE_FORMAT, FILES_API} from '../constants';
import DescriptionList from 'ant-design-pro/lib/DescriptionList';
import routes from './routes';
import {abandonOrder, effectiveOrder} from './api';
import {injectIntl} from './intl';
import {testDriveStatus, gender, evaluateResult, ReturnStatus, customerOccupationType} from '../Enum';
import {conventEnumValueToString, formatDateTime} from '../utils';
import {hasPermissions} from './utils';
import {PERMISSION} from './constants';
import WrappedPopconfirm from '../common/WrappedPopconfirm';
import styles from './style.css';
const {Description} = DescriptionList;
export class DetailPanel extends React.PureComponent {
    componentDidMount() {
        this.props.init();
    }
    handleUpdateClick = () => {
        this.props.history.push(routes.update.format(this.props.id));
    }
    onClickAbandonBtn = () => {
        this.setState({loading: true});
        abandonOrder(this.props.detail.id, this.props.detail.rowVersion)
            .then(res => {
                this.setState({loading: false});
                if(res.ok)
                    this.props.successReturn();
            });
    }
    onClickEffectiveBtn = () => {
        this.setState({loading: true});
        effectiveOrder(this.props.detail.id, this.props.detail.rowVersion)
            .then(res => {
                this.setState({loading: false});
                if(res.ok)
                    this.props.successReturn();
            });
    }
    
    render() {
        const {formatMessage} = this.props.intl;
        const {options, attachments = []} = this.props.detail;
        const operations = [];
        if(hasPermissions(this.props.permissions, PERMISSION.update) && hasPermissions(options, PERMISSION.update))
            operations.push(<Button key="update" onClick={this.handleUpdateClick} size="small">{
                formatMessage({
                    id: 'detailPage.btn.update',
                    defaultMessage: '编辑'
                })
            }</Button>);
        if(hasPermissions(this.props.permissions, PERMISSION.abandon) && hasPermissions(options, PERMISSION.abandon))
            operations.push(<WrappedPopconfirm
                key="abandon"
                id={this.props.id}
                placement="topLeft"
                onConfirm={this.onClickAbandonBtn}
                title={formatMessage({
                    id: 'detailPanel.validate.abandon',
                    defaultMessage: '是否确认作废？'
                })}>
                <Button key="abandon" size="small">{
                    formatMessage({
                        id: 'detailPanel.btn.abandon',
                        defaultMessage: '作废'
                    })
                }</Button>
            </WrappedPopconfirm>);
        if(hasPermissions(this.props.permissions, PERMISSION.effective) && hasPermissions(options, PERMISSION.effective))
            operations.push(<WrappedPopconfirm
                key="effective"
                id={this.props.id}
                placement="topLeft"
                onConfirm={this.onClickEffectiveBtn}
                title={formatMessage({
                    id: 'detailPanel.validate.effective',
                    defaultMessage: '是否确认生效？'
                })}>
                <Button key="effective" size="small">{
                    formatMessage({
                        id: 'detailPanel.btn.effective',
                        defaultMessage: '生效'
                    })
                }</Button>
            </WrappedPopconfirm>);
        return (
            <div className="detail">
                <Card loading={this.props.loading}>
                    <DescriptionList size="large" title={<Row><Col span={12}>{formatMessage({
                        id: 'detailPanel.customer.title',
                        defaultMessage: '客户信息'
                    })}</Col>
                    <Col className={`col-align-right ${styles.detail}`} span={12}>{operations}</Col>
                    </Row>}>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.code',
                            defaultMessage: '试乘试驾编号'
                        })}>{this.props.detail.code}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.brandName',
                            defaultMessage: '品牌名称'
                        })}>{this.props.detail.brandName}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.dealerCode',
                            defaultMessage: '总代编号'
                        })}>{this.props.detail.dealerCode}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.dealerName',
                            defaultMessage: '总代名称'
                        })}>{this.props.detail.dealerName}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.subDealerCode',
                            defaultMessage: '二级服务站编号'
                        })}>{this.props.detail.subDealerCode}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.subDealerName',
                            defaultMessage: '二级服务站名称'
                        })}>{this.props.detail.subDealerName}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.country',
                            defaultMessage: '国家'
                        })}>{this.props.detail.country}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.testDriveYear',
                            defaultMessage: '试乘试驾年份'
                        })}>{this.props.detail.testDriveYear}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.testDriveMonth',
                            defaultMessage: '试乘试驾月份'
                        })}>{this.props.detail.testDriveMonth}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.customerCode',
                            defaultMessage: '客户编号'
                        })}>{this.props.detail.customerCode}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.customerName',
                            defaultMessage: '客户姓名'
                        })}>{this.props.detail.customerName}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.cellPhoneNumber',
                            defaultMessage: '客户电话'
                        })}>{this.props.detail.cellPhoneNumber}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.gender',
                            defaultMessage: '性别'
                        })}>{conventEnumValueToString(gender, this.props.detail.gender)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.birthday',
                            defaultMessage: '生日'
                        })}>{formatDateTime(this.props.detail.birthday, DATE_FORMAT)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.purchaseOrNot',
                            defaultMessage: '是否购车'
                        })}>{this.props.detail.purchaseOrNot ? formatMessage({
                                id: 'detailPanel.text.yes',
                                defaultMessage: '是'
                            }) : formatMessage({
                                id: 'detailPanel.text.no',
                                defaultMessage: '否'
                            })}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.trackCustomerOrNot',
                            defaultMessage: '是否跟踪'
                        })}>{this.props.detail.trackCustomerOrNot ? formatMessage({
                                id: 'detailPanel.text.yes',
                                defaultMessage: '是'
                            }) : formatMessage({
                                id: 'detailPanel.text.no',
                                defaultMessage: '否'
                            })}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.operation.estimatedTimeForDeal',
                            defaultMessage: '预计成交时间'
                        })}>{formatDateTime(this.props.detail.estimatedTimeForDeal, DATE_FORMAT)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.dealOrNot',
                            defaultMessage: '是否成交'
                        })}>{this.props.detail.dealOrNot ? formatMessage({
                                id: 'detailPanel.text.yes',
                                defaultMessage: '是'
                            }) : formatMessage({
                                id: 'detailPanel.text.no',
                                defaultMessage: '否'
                            })}</Description>
                    </DescriptionList>
                    <Divider/>
                    <DescriptionList size="large" title={<Row><Col span={12}>{formatMessage({
                        id: 'detailPanel.car.title',
                        defaultMessage: '车辆信息'
                    })}</Col>
                    </Row>}>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.productCode',
                            defaultMessage: '产品编号'
                        })}>{this.props.detail.productCode}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.productName',
                            defaultMessage: '产品描述'
                        })}>{this.props.detail.productName}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.modelCode',
                            defaultMessage: '车型代码'
                        })}>{this.props.detail.modelCode}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.modelName',
                            defaultMessage: '车型名称'
                        })}>{this.props.detail.modelName}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.VIN',
                            defaultMessage: 'VIN'
                        })}>{this.props.detail.vIN}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.saleConfirmationCode',
                            defaultMessage: '销售确认书编号'
                        })}>{this.props.detail.saleConfirmationCode}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.localLicensePlateNumber',
                            defaultMessage: '当地上牌号码'
                        })}>{this.props.detail.localLicensePlateNumber}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.useVehicleStickerOrNot',
                            defaultMessage: '是否使用车贴'
                        })}>{this.props.detail.useVehicleStickerOrNot ? formatMessage({
                                id: 'detailPanel.text.yes',
                                defaultMessage: '是'
                            }) : formatMessage({
                                id: 'detailPanel.text.no',
                                defaultMessage: '否'
                            })}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.keepTestDrivePhotoOrNot',
                            defaultMessage: '是否提供试驾车照片'
                        })}>{this.props.detail.keepTestDrivePhotoOrNot ? formatMessage({
                                id: 'detailPanel.text.yes',
                                defaultMessage: '是'
                            }) : formatMessage({
                                id: 'detailPanel.text.no',
                                defaultMessage: '否'
                            })}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.operation.putIntoUseTime',
                            defaultMessage: '投入使用时间'
                        })}>{formatDateTime(this.props.detail.putIntoUseTime, DATE_FORMAT)}</Description>
                            
                        <Description term={formatMessage({
                            id: 'detailPanel.text.fOBPercentImplementedOrNot',
                            defaultMessage: 'FOB比例是否兑现'
                        })}>{this.props.detail.fOBPercentImplementedOrNot ? formatMessage({
                                id: 'detailPanel.text.yes',
                                defaultMessage: '是'
                            }) : formatMessage({
                                id: 'detailPanel.text.no',
                                defaultMessage: '否'
                            })}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.operation.timeForImplementation',
                            defaultMessage: '费用支持兑现时间'
                        })}>{formatDateTime(this.props.detail.timeForImplementation, DATE_FORMAT)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.operation.timeOfUse',
                            defaultMessage: '使用时间'
                        })}>{formatDateTime(this.props.detail.timeOfUse, DATE_FORMAT)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.drivingMileage',
                            defaultMessage: '借车里程表数'
                        })}>{this.props.detail.drivingMileage}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.operation.timeOfReturn',
                            defaultMessage: '归还时间'
                        })}>{formatDateTime(this.props.detail.timeOfReturn, DATE_FORMAT)}</Description>
                             
                        <Description term={formatMessage({
                            id: 'detailPanel.text.returnStatus',
                            defaultMessage: '归还状态'
                        })}>{conventEnumValueToString(ReturnStatus, this.props.detail.returnStatus)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.mileageWhenReturn',
                            defaultMessage: '还车里程表数'
                        })}>{this.props.detail.mileageWhenReturn}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.remark',
                            defaultMessage: '备注'
                        })}>{this.props.detail.remark}</Description>
                    </DescriptionList>
                    <Divider/>
                    <DescriptionList size="large" title={<Row><Col span={12}>{formatMessage({
                        id: 'detailPanel.merit.title',
                        defaultMessage: '试乘试驾评估信息'
                    })}</Col>
                    </Row>}>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.occupationType',
                            defaultMessage: '客户职业类型'
                        })}>{conventEnumValueToString(customerOccupationType, this.props.detail.occupationType)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.drivingAge',
                            defaultMessage: '驾龄'
                        })}>{this.props.detail.drivingAge}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.salesConsultantName',
                            defaultMessage: '销售顾问姓名'
                        })}>{this.props.detail.salesConsultantName}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.evaluationDate',
                            defaultMessage: '评估日期'
                        })}>{formatDateTime(this.props.detail.evaluationDate, DATE_FORMAT)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.dynamicsPerformance',
                            defaultMessage: '动力表现'
                        })}>{conventEnumValueToString(evaluateResult, this.props.detail.dynamicsPerformance)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.maneuverability',
                            defaultMessage: '操控性能'
                        })}>{conventEnumValueToString(evaluateResult, this.props.detail.maneuverability)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.brakePerformance',
                            defaultMessage: '制动性能'
                        })}>{conventEnumValueToString(evaluateResult, this.props.detail.brakePerformance)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.drivingComfort',
                            defaultMessage: '舒适性能'
                        })}>{conventEnumValueToString(evaluateResult, this.props.detail.drivingComfort)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.interiorSpace',
                            defaultMessage: '内部乘坐空间感受'
                        })}>{conventEnumValueToString(evaluateResult, this.props.detail.interiorSpace)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.testDriveService',
                            defaultMessage: '服务满意度'
                        })}>{conventEnumValueToString(evaluateResult, this.props.detail.testDriveService)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.conditionAndCleanliness',
                            defaultMessage: '车况和清洁程度'
                        })}>{conventEnumValueToString(evaluateResult, this.props.detail.conditionAndCleanliness)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.routeLength',
                            defaultMessage: '路线的长度'
                        })}>{conventEnumValueToString(evaluateResult, this.props.detail.routeLength)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.driveRoute',
                            defaultMessage: '路线设置'
                        })}>{conventEnumValueToString(evaluateResult, this.props.detail.driveRoute)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.overallMerit',
                            defaultMessage: '综合评价'
                        })}>{conventEnumValueToString(evaluateResult, this.props.detail.overallMerit)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.testDriveMeaningfulToPurchasing',
                            defaultMessage: '乘试驾对购车是否有作用'
                        })}>{this.props.detail.testDriveMeaningfulToPurchasing ? formatMessage({
                                id: 'detailPanel.text.yes',
                                defaultMessage: '是'
                            }) : formatMessage({
                                id: 'detailPanel.text.no',
                                defaultMessage: '否'
                            })}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.testDriveStatus',
                            defaultMessage: '试乘试驾信息状态'
                        })}>{conventEnumValueToString(testDriveStatus, this.props.detail.testDriveStatus)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.operation.creatorName',
                            defaultMessage: '创建人姓名'
                        })}>{this.props.detail.creatorName}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.operation.createTime',
                            defaultMessage: '创建时间'
                        })}>{formatDateTime(this.props.detail.createTime, DATE_FORMAT)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.operation.modifierName',
                            defaultMessage: '更新人姓名'
                        })}>{this.props.detail.modifierName}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.operation.modifyTime',
                            defaultMessage: '更新时间'
                        })}>{formatDateTime(this.props.detail.modifyTime, DATE_FORMAT)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.operation.effectPersonName',
                            defaultMessage: '生效人姓名'
                        })}>{this.props.detail.effectPersonName}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.operation.effectTime',
                            defaultMessage: '生效时间'
                        })}>{formatDateTime(this.props.detail.effectTime, DATE_FORMAT)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.operation.abandonerName',
                            defaultMessage: '作废人姓名'
                        })}>{this.props.detail.abandonerName}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.operation.abandonTime',
                            defaultMessage: '作废时间'
                        })}>{formatDateTime(this.props.detail.abandonTime, DATE_FORMAT)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.opinionsAndSuggestions',
                            defaultMessage: '意见及建议'
                        })}>{this.props.detail.opinionsAndSuggestions}</Description>
                    </DescriptionList>
                    <Divider/>
                    {attachments && attachments.length > 0
                        ? <DescriptionList title={formatMessage({
                            id: 'detailPage.card.attachment',
                            defaultMessage: '附件清单'
                        })} size="large">
                            {attachments.map(item => (<Description key={item.fileId}>
                                <a icon="download" download href={`${FILES_API.files}/${item.fileId}`} size="small">
                                    {item.fileName}</a></Description>))}
                        </DescriptionList>
                        : formatMessage({
                            id: 'detailPage.content.noAttachment',
                            defaultMessage: '暂无附件'
                        })}
                </Card>
                <div className="page-toolbar">
                    <Button
                        type="primary"
                        disabled={this.props.loading}
                        onClick={this.props.onCancel}>{formatMessage({
                            id: 'detailPage.text.return',
                            defaultMessage: '返回'
                        })}</Button>
                </div>
            </div>
        );
    }
}

DetailPanel.propTypes = {
    detail: PropTypes.object.isRequired,
    id: PropTypes.string.isRequired,
    init: PropTypes.func.isRequired,
    loading: PropTypes.bool.isRequired,
    permissions: PropTypes.array.isRequired,
    onCancel: PropTypes.func.isRequired,
    history: PropTypes.object,
    intl: PropTypes.object,
    successReturn: PropTypes.func,
};

import {connect} from 'react-redux';
import {
    getDetail,
    tableSearch
} from './actions';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
const getPermissions = selectorFactory(['page', 'domainData', 'permission', 'data']);
const getDetailData = selectorFactory(['page', 'domainData', 'detail', 'data']);


const mapStateToProps = (state, props) => ({
    id: props.id,
    loading: state.getIn(['page', 'domainData', 'detail', 'isFetching']),
    detail: getDetailData(state),
    permissions: getPermissions(state),
});
const mapDispatchToProps = (dispatch, props) => ({
    init: () => dispatch(getDetail(props.id)),
    successReturn: () => {
        dispatch(tableSearch());
        props.history.push(routes.query.url());
    },
    onCancel: () => {
        props.history.push(routes.query.url());
    },
});
export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(DetailPanel));
